View = {
	
	loadTrip: function(){
		$.ajax({
			url:"/loadtrip",
			data:{
				trip : $(this).val()
			},
			success: function(data){
				data = $.parseJSON(data);
				var mapdata = {};
				if(data.destinations[0])
					mapdata.start = data.destinations[0].name;
				if(data.destinations.length > 1)
					mapdata.end = data.destinations[data.destinations.length -1].name;
				mapdata.waypoints = [];
				for(var i = 1; i < data.destinations.length - 1; i++)
					mapdata.waypoints.push({
						location : data.destinations[i].name,
						stopover : true
						});
				Maps.buildMap(mapdata);
			},
			type: "POST"
		});
	}
		
}

$(function() {
	
	$("#tripPicker").change(View.loadTrip).change();
	
	$("#editTrip").click(function(){
		location = "/edit.jsp?trip=" + $("#tripPicker").val();
	});
	
	$("#tripDetails").click(function(){
		location = "/tripDetails.jsp?trip=" + $("#tripPicker").val();
	});
	
});
